package com.shouxin.attendance.network;

import org.apache.log4j.Logger;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public final class ServiceFactory {

	private static final Logger logger = Logger.getLogger(ServiceFactory.class);

	private Map<String, Object> serviceMap = Collections.synchronizedMap(new HashMap<String, Object>());

	private ServiceFactory() {
	}

	public static ServiceFactory get() {
		return Instance.instance;
	}

	public <T> T getService(String name) {
		try {
			Object clz = serviceMap.get(name);
			if (clz == null) {
				clz = Class.forName(name).newInstance();
				serviceMap.put(name, clz);
			}
			return (T) clz;
		} catch (Exception e) {
			logger.error("getService exception for name is " + name, e);
		}
		return null;
	}

	private static class Instance {

		static ServiceFactory instance = new ServiceFactory();
	}
}
